package com.xiyue.server.config;


import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.xiyue.server.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class StpInterfaceImpl implements StpInterface {
    @Resource
    private SysUserService sysUserService;
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> permissionList = new ArrayList<>();
        // 迭代角色列表 - 根据角色code获得权限code
        for(String roleCode: getRoleList(loginId,loginType)){
            SaSession session = SaSessionCustomUtil.getSessionById("role-" + roleCode);
            List<String> permissions = session.get("Permission_List", () -> sysUserService.listPermissionByRoleCode(roleCode));
            permissionList.addAll(permissions);
        }
        return permissionList;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        SaSession sessionByLoginId = StpUtil.getSessionByLoginId(loginId.toString());
        return sessionByLoginId.get("Role_List", () -> sysUserService.listRoleByUserId(loginId.toString()));
    }
}
